package com.intcast;

import java.util.Random;
import java.util.Scanner;

/*
七色球案例第二次练习
*/
public class Demo11 {
    public static void main(String[] args) {
        int[] userArr = userBalls();
        System.out.println(printArr(userArr));
        int[] systemBalls = systemBalls();
        System.out.println(printArr(systemBalls));
        int resout = star(userArr, systemBalls);
        System.out.println("获奖为：" + resout + "元");

    }

    //开始比较的方法
    public static int star(int[] userArr, int[] systemArr) {
        //定义两个数存放红球和蓝球相同的个数
        int redCount = 0;
        int blueCount = 0;
        for (int i = 0; i < userArr.length - 1; i++) {
            for (int j = 0; j < systemArr.length - 1; j++) {
                if (userArr[i] == systemArr[j]) {
                    redCount++;
                }
            }
        }
        blueCount = userArr[userArr.length - 1] == systemArr[systemArr.length - 1] ? 1 : 0;
        if (redCount + blueCount == 7) {
            return 10000000;
        } else if (redCount == 6) {
            return 5000000;
        } else if (redCount + blueCount == 6) {
            return 3000;
        } else if (redCount + blueCount == 5) {
            return 200;
        } else if (redCount + blueCount == 4) {
            return 10;
        } else if (blueCount == 1) {
            return 5;
        } else {
            return 0;
        }
    }

    //用户输入七个数
    public static int[] userBalls() {
//        用来存放用户输入的数
        int[] userArr = new int[7];
        Scanner sc = new Scanner(System.in);
        for (int i = 0; i < userArr.length - 1; i++) {
            while (true) {
                String str = printArr(userArr);
                System.out.println("请输入第" + (i + 1) + "个红色球的数，范围为(1~33),目前的数组为：" + str);
                int userNumber = sc.nextInt();
                //用来判断范围是否合理
                if (userNumber <= 33 && userNumber >= 1) {
                    userArr[i] = userNumber;
                    break;
                } else {
                    System.out.println("你输的数字无效请重新输入：");
                }
            }
        }
        while (true) {
            System.out.println("请输入蓝色球的号码,范围为(1~16):");
            int userblue = sc.nextInt();
            if (userblue >= 1 && userblue <= 16) {
                userArr[userArr.length - 1] = userblue;
                break;
            } else {
                System.out.println("你输入数字无效，请重新输入：");
            }
        }
        return userArr;
    }

    //系统生成7个随机数
    private static int[] systemBalls() {
        int[] systemArr = new int[7];
        Random r = new Random();
        for (int i = 0; i < systemArr.length - 1; i++) {

            while (true) {
                int number = r.nextInt(33) + 1;
                //用来比较生成的随机数在数组里面有没有
                for (int j = 0; j < systemArr.length - 1; j++) {
                    if (number != systemArr[i]) {
                        systemArr[i] = number;
                        break;
                    }
                }
                int systemblueballs = r.nextInt(16) + 1;
                if (systemblueballs >= 1 && systemblueballs <= 16) {
                    systemArr[systemArr.length - 1] = systemblueballs;
                    break;
                }
            }
        }
        return systemArr;
    }

    //用来打印出数组
    public static String printArr(int[] arr) {
        String str = "[";
        for (int i = 0; i < arr.length; i++) {
            if (i == arr.length - 1) {
                str += arr[i];
            } else {
                str += arr[i] + ",";
            }
        }
        str += "]";
        return str;
    }
}
